﻿package mortal.game.scene3D.layer3D.utils
{
    import baseEngine.basic.*;
    import baseEngine.core.*;
    import baseEngine.modifiers.*;
    import com.gengine.debug.*;
    import com.gengine.utils.*;
    import com.greensock.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import frEngine.*;
    import frEngine.event.*;
    import frEngine.pools.*;
    import mortal.game.*;
    import mortal.game.scene3D.fight.*;
    import mortal.game.scene3D.layer3D.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.scene3D.map3D.sceneInfo.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.scene3D.player.entity.*;

    public class EffectUtil extends EntityLayerUtil
    {
        private var _map:Dictionary;

        public function EffectUtil(param1:PlayerLayer)
        {
            this._map = new Dictionary();
            super(param1);
            return;
        }// end function

        public function getEffect(param1:String) : Pivot3D
        {
            return this._map[param1];
        }// end function

        private function addEffectToMap(param1:String, param2:Pivot3D) : void
        {
            this._map[param1] = param2;
            return;
        }// end function

        public function addEffect(param1:SceneEffectData) : Pivot3D
        {
            var _loc_3:Pivot3D = null;
            var _loc_4:String = null;
            var _loc_5:RenderList = null;
            var _loc_6:Boolean = false;
            var _loc_2:* = param1.effectName;
            if (_loc_2)
            {
                _loc_3 = this.getEffect(param1.key) as Pivot3D;
                _loc_4 = _loc_2.split(".")[0];
                _loc_5 = null;
                if (param1.isOverShadowArea)
                {
                    _loc_5 = LastRender.instance.renderList;
                }
                _loc_6 = _loc_2.indexOf(".xml") > -1;
                if (_loc_3 == null)
                {
                    if (!_loc_6)
                    {
                        _loc_3 = EffectPlayerPool.instance.getEffectPlayer(_loc_4, this.getEffectParent(), _loc_5, true);
                    }
                    else
                    {
                        _loc_3 = FrObjectPool.getObject(GroupEffectPlayer, "EffectUtils");
                        (_loc_3 as GroupEffectPlayer).load(_loc_2, true, _loc_5, this.getEffectParent());
                    }
                    this.addEffectToMap(param1.key, _loc_3);
                }
                else if (!_loc_6)
                {
                    (_loc_3 as EffectPlayer).renderList = _loc_5;
                }
                if (!_loc_6)
                {
                    (_loc_3 as EffectPlayer).data = param1;
                }
                (_loc_3 as IGame2D).play(PlayMode.ANIMATION_LOOP_MODE);
                (_loc_3 as IGame2D).x2d = param1.x;
                (_loc_3 as IGame2D).y2d = param1.y;
                if (!_loc_6 && (param1.isRandomPlaceType || param1.isAccordingShowType))
                {
                    FrEventDispatcher.instance.proxyAddEventListener(_loc_3, Engine3dEventName.PLAYEND, this.onEffectPlayEnd);
                }
                Log.debug("增加特效:" + param1.effectName, _loc_2);
                return _loc_3;
            }
            return null;
        }// end function

        private function onEffectPlayEnd(event:Event) : void
        {
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_2:* = event.currentTarget as EffectPlayer;
            var _loc_3:* = _loc_2.data as SceneEffectData;
            _loc_2.data = null;
            if (_loc_3)
            {
                if (_loc_3.isRandomPlaceType)
                {
                    _loc_4 = MathUitl.random(_loc_3.x - _loc_3.r, _loc_3.x + _loc_3.r);
                    _loc_5 = MathUitl.random(_loc_3.y - _loc_3.r, _loc_3.y + _loc_3.r);
                    _loc_2.x2d = _loc_4;
                    _loc_2.y2d = _loc_5;
                }
                else if (_loc_3.isAccordingShowType && !GameMapUtil.curMapState.isQuestionCopy)
                {
                    _loc_2.stop();
                }
            }
            return;
        }// end function

        public function removeEffect(param1:String) : void
        {
            var _loc_2:* = this.getEffect(param1);
            if (_loc_2)
            {
                delete this._map[param1];
                FrEventDispatcher.instance.proxyRemoveEventListener(_loc_2, Engine3dEventName.PLAYEND, this.onEffectPlayEnd);
                _loc_2.parent = null;
                _loc_2.dispose();
            }
            return;
        }// end function

        public function getEffectParent(param1:Boolean = false) : Pivot3D
        {
            if (!param1)
            {
                return Game.scene.playerLayer;
            }
            return null;
        }// end function

        override public function removeAll() : void
        {
            var _loc_1:Pivot3D = null;
            for each (_loc_1 in this._map)
            {
                
                _loc_1.dispose();
            }
            this._map = new Dictionary();
            return;
        }// end function

        override public function updateEntity() : void
        {
            var _loc_2:Pivot3D = null;
            var _loc_3:Point = null;
            var _loc_4:SceneEffectData = null;
            var _loc_5:Boolean = false;
            var _loc_6:Boolean = false;
            var _loc_7:Boolean = false;
            if (!Game.sceneInfo)
            {
                return;
            }
            var _loc_1:* = Game.sceneInfo.effectInfos;
            if (_loc_1.length < 1)
            {
                return;
            }
            for each (_loc_4 in _loc_1)
            {
                
                _loc_5 = SceneRange.isInEffectRange(_loc_4.x, _loc_4.y);
                _loc_6 = FightEffectUtil.isShieldEffect();
                _loc_7 = this.isInAccordingArea(_loc_4);
                if (_loc_5 && (_loc_7 || !_loc_4.isAccordingShowType && !_loc_6))
                {
                    _loc_2 = this.getEffect(_loc_4.key);
                    if (_loc_2 == null)
                    {
                        _loc_2 = this.addEffect(_loc_4);
                    }
                    else
                    {
                        if (_loc_2.isDisposed || _loc_2.isInPool)
                        {
                            this.removeEffect(_loc_4.key);
                            _loc_2 = this.addEffect(_loc_4);
                        }
                        if (_loc_2.parent == null)
                        {
                            _loc_2.parent = this.getEffectParent();
                        }
                    }
                    continue;
                }
                this.removeEffect(_loc_4.key);
            }
            return;
        }// end function

        private function isInAccordingArea(param1:SceneEffectData) : Boolean
        {
            return param1.isAccordingShowType && param1.showRec.contains(RolePlayer.instance.x2d, RolePlayer.instance.y2d);
        }// end function

        public static function wiggle(param1:DisplayObject) : void
        {
            var timeLite:TimelineLite;
            var obj:* = param1;
            var x:* = obj.x;
            var y:* = obj.y;
            timeLite = new TimelineLite();
            timeLite.append(new TweenLite(obj, 0.02, {x:x - 7, y:y}));
            timeLite.append(new TweenLite(obj, 0.02, {x:x, y:y + 5}));
            timeLite.append(new TweenLite(obj, 0.02, {x:x + 7, y:y}));
            timeLite.append(new TweenLite(obj, 0.02, {x:x, y:y - 5}));
            timeLite.append(new TweenLite(obj, 0.02, {x:x - 7, y:y}));
            timeLite.append(new TweenLite(obj, 0.02, {x:x, y:y + 5}));
            timeLite.append(new TweenLite(obj, 0.02, {x:x + 7, y:y}));
            timeLite.append(new TweenLite(obj, 0.02, {x:x, y:y - 5}));
            timeLite.append(new TweenLite(obj, 0.02, {x:x - 7, y:y}));
            timeLite.append(new TweenLite(obj, 0.02, {x:x, y:y + 5}));
            timeLite.append(new TweenLite(obj, 0.02, {x:x + 7, y:y}));
            timeLite.append(new TweenLite(obj, 0.02, {x:x, y:y - 5}));
            timeLite.append(new TweenLite(obj, 0.02, {x:x, y:y, onComplete:function () : void
            {
                timeLite.stop();
                timeLite.kill();
                timeLite = null;
                return;
            }// end function
            }));
            timeLite.play();
            return;
        }// end function

    }
}
